//
//  AYMWeatherView.m
//  QuickCure
//
//   Mr.LV
//

#import "AYMWeatherView.h"
#import "AYMWeatherModel.h"
#import "AYMMainWeatherModel.h"

@interface AYMWeatherView()
//图片
@property (nonatomic, weak) UIImageView *weatherImage;

//天气
@property (nonatomic, weak) UILabel *weatherLabel;

//温度
@property (nonatomic, weak) UILabel *showTemperatureLabel;

//时间
@property (nonatomic, weak) UILabel *timeLabel;

@property (nonatomic, weak) UIView *lineView;

@end

@implementation AYMWeatherView

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
    
        [self setupUI];
    }
    return self;
}

-(void)setupUI{

    self.backgroundColor = [UIColor colorWithRed:185/255.0 green:235/255.0 blue:216/255.0 alpha:1];
    
    self.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *weatherGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didAreaLabelClick:)];
    
    [self addGestureRecognizer:weatherGes];
    
    
    [self setsChildLabel];
    

}


#pragma mark - 设置子控件
- (void)setsChildLabel {
    
    //分隔线
    UIView *lineView = [[UIView alloc]init];
    
    lineView.backgroundColor = [UIColor lightGrayColor];
    self.lineView = lineView;
    
    [self addSubview:lineView];
    
    //天气图片
    UIImageView *weatherImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yin"]];
    self.weatherImage = weatherImage;
    
    [self addSubview:weatherImage];
    
    [weatherImage makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(30);
        make.height.equalTo(27);
        make.leading.equalTo(5);
        make.centerY.equalTo(self);
    }];
    
    
    // 天气label
    UILabel *weatherLabel = [self addChildLabel:@"多云"];
    self.weatherLabel = weatherLabel;
    
    [weatherLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self);
        
        make.leading.equalTo(weatherImage.trailing).offset(5);
        
    }];
    
    
    
    // 地区
    UILabel *areaLabel = [self addChildLabel:@"上海"];
    areaLabel.userInteractionEnabled = YES;
    self.areaLabel = areaLabel;
    //点击手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didAreaLabelClick:)];
    
    [areaLabel addGestureRecognizer:tapGes];
    
    
    //约束
    [areaLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self);
        
        make.trailing.equalTo(lineView.leading).offset(-10);
        
    }];
    
    //下滑线
    UIView *areaLineView = [[UIView alloc] init];
    areaLineView.backgroundColor = random(80, 201, 197, 255);
    
    [self addSubview:areaLineView];
    
    [areaLineView makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(1);
        make.width.equalTo(areaLabel.width);
        make.top.equalTo(areaLabel.bottom).offset(-2);
        make.leading.equalTo(areaLabel.leading);
    }];
    
    
    
    // 温度
    UILabel *temperatureLabel = [self addChildLabel:@"温度:"];
    
    [temperatureLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self).offset(-12);
        make.leading.equalTo(lineView.trailing).offset(10);
        
    }];
    
    //时间
    UILabel *timeLabel = [self addChildLabel:@"2016年05月05日"];
    
    self.timeLabel = timeLabel;
    [timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(12);
        make.leading.equalTo(lineView.trailing).offset(10);
        
    }];
    
    
    //温度显示
    UILabel *showTemperatureLabel = [self addChildLabel:@"27 ~ 26度"];
    
    self.showTemperatureLabel = showTemperatureLabel;
    [showTemperatureLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(temperatureLabel);
        
        make.leading.equalTo(temperatureLabel.trailing).offset(10);
    }];
    
    
    
}


-(void)layoutSubviews{

    [super layoutSubviews];
    
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(self.frame.size.height - 20);
        //        NSLog(@"%f",self.frame.size.height)
        make.width.equalTo(1);
        make.center.equalTo(self);
        
    }];
   
    
//    UILabel *weatherLabel = [[UILabel alloc]init];
//    [self addSubview:weatherImage];
    
   
    
}

#pragma mark - 加载天气数据
- (void)reloadDatas:(AYMMainWeatherModel *)weatherModel {
    
    AYMWeatherModel *model = weatherModel.weather_data[0];
    
    //判断什么天气显示什么图片
    
    
    [self.weatherImage sd_setImageWithURL:[NSURL URLWithString:model.dayPictureUrl]];
    
    
    if (model.weather == nil) {
        [SVProgressHUD showInfoWithStatus:@"没有收录这个地方的天气"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismissWithDelay:0.5];
        });
    }else {
        
        //天气
        self.weatherLabel.text = model.weather;
        
    }
    
    
    
    if (weatherModel.currentCity == nil) {
        [SVProgressHUD showInfoWithStatus:@"没有收录这个地方的天气"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismissWithDelay:0.5];
        });
    }else {
        
        //地区
        self.areaLabel.text = weatherModel.currentCity;
    }
    
    
    
    if (model.temperature == nil) {
        [SVProgressHUD showInfoWithStatus:@"没有收录这个地方的天气"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismissWithDelay:0.5];
        });
    }else {
        
        //温度
        self.showTemperatureLabel.text = model.temperature;
    }
    
    if (model.temperature == nil) {
        [SVProgressHUD showInfoWithStatus:@"没有收录这个地方的天气"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismissWithDelay:0.5];
        });
    }else {
        
        //时间
        self.timeLabel.text = [NSString stringWithFormat:@"日期: %@",model.date];
    }
    

    
}

#pragma mark - 添加天气label
- (UILabel *)addChildLabel:(NSString *)labelText{
    
    UILabel *label = [[UILabel alloc] init];
    
    label.text = labelText;
    
    label.textColor = random(80, 201, 197, 255);
    
    label.font = [UIFont systemFontOfSize:homeFont];
    
    [self addSubview:label];
    
    return label;
}

#pragma mark - 地区label点击事件
- (void)didAreaLabelClick: (UITapGestureRecognizer *) tapGes{
    
    
    //判断代理是否实现了这个方法
    if ([self.areaDelegate respondsToSelector:@selector(addAreaViewController)]) {
        
        [self.areaDelegate addAreaViewController];
        
    }
    
//    Log(@"地区按钮点击");
    
    
}


@end
